package com.gxa.modules.service.lx.impl;

import com.gxa.common.constant.PageInfo;
import com.gxa.common.vo.R2;
import com.gxa.modules.mapper.lx.GoodsMapper;
import com.gxa.modules.mapper.lx.OrderManageMapper;
import com.gxa.modules.service.lx.OrderManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class OrderManageServiceImpl implements OrderManageService {

    @Autowired(required = false)
    private OrderManageMapper orderManageMapper  ;

    @Override
    public R2 queryOrderList(Map map) {

        if(map.get("nowPage") == null && map.get("pageSize") == null){
            map.put("nowPage", PageInfo.NOWPAGE);
            map.put("pageSize",PageInfo.PAGESIZE);
        }
        Integer nowPage = ((Integer) map.get("nowPage")-1)*(Integer)map.get("pageSize");
        map.put("nowPage",nowPage);
        Integer sumPage = orderManageMapper.sumPage(map);

        List<Map> orderlist = orderManageMapper.queryOrderList(map);
        List list = new ArrayList();
        list.add(sumPage);
        if(map.get("type") == null){
            map.put("type",1);
            Integer Page1 = orderManageMapper.sumPage(map);
            map.put("type",2);
            Integer Page2 = orderManageMapper.sumPage(map);
            list.add(Page1);
            list.add(Page2);
        }
        list.add(orderlist);
        R2 r2 = new R2();
        if(list != null){
            r2.setCode(200);
            r2.setMsg("查询成功");
            r2.setData(list);
        }else{
            r2.setCode(500);
            r2.setMsg("系统开小差了");
        }
        return r2;
    }

    @Override
    public R2 queryInfo(Map map) {

        Set orderidlist = orderManageMapper.queryOrderIdList(map);
        Set goodsnamelist = orderManageMapper.queryOrderGoodsNameList(map);
        Set vipnamelist = orderManageMapper.queryOrderVipNameList(map);

        List list = new ArrayList();
        list.add(orderidlist);
        list.add(goodsnamelist);
        list.add(vipnamelist);
        R2 r2 = new R2();
        if(list != null){
            r2.setCode(200);
            r2.setMsg("查询成功");
            r2.setData(list);
        }else{
            r2.setCode(500);
            r2.setMsg("系统开小差了");
        }
        return r2;
    }

    @Override
    public R2 change(Map map) {
        R2 r2 = new R2();
        Integer result = 0;
        if(map.get("userId")!=null){
            result = orderManageMapper.deliver(map);
            Integer result1 = orderManageMapper.changeUserMoney(map);
            if(result != 0 && result1 != 0){
                r2.setCode(200);
                r2.setMsg("修改成功");
            }else{
                r2.setCode(500);
                r2.setMsg("系统开小差了");
            }
        }else if(map.get("address")!=null){
            result = orderManageMapper.changeAddress(map);
        } else if(map.get("remark")!=null){
            result = orderManageMapper.remark(map);
        }
        if(result != 0 ){
            r2.setCode(200);
            r2.setMsg("修改成功");
        }else{
            r2.setCode(500);
            r2.setMsg("系统开小差了");
        }
        return r2;
    }


}
